<?php

declare(strict_types=1);

namespace app\admin\controller\v1\pageConfig;

use app\common\Backend;
use app\common\model\VipDay;
use app\common\model\VipPrice;
use think\Request;

class VipMoeny extends Backend
{
    /**
     * @var Navigation
     */
    protected $model = null;

    protected $quickSearchField = ['title'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new VipPrice();
    }

    /**
     * 数据源
     * @throws Throwable
     */
    public function index(): void
    {
        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => '',
        ]);
    }

    //重写add
    public function add(): void
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $day = VipDay::find(intval($data['vip_day_id']));
            
            if (!$data) {
                $this->error(('Parameter %s can not be empty'));
            }

            $data = $this->excludeFields($data);
            if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                $data[$this->dataLimitField] = $this->auth->id;
            }
            $data['min_price'] = sprintf("%.2f",floor($data['price']) / $day['day']);
            $result = false;
            $this->model->startTrans();
            try {
                // 模型验证
                if ($this->modelValidate) {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    if (class_exists($validate)) {
                        $validate = new $validate;
                        if ($this->modelSceneValidate) $validate->scene('add');
                        $validate->check($data);
                    }
                }
                $result = $this->model->save($data);
                $this->model->commit();
            } catch (Throwable $e) {
                $this->model->rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success(('Added successfully'));
            } else {
                $this->error(('No rows were added'));
            }
        }

        $this->error(('Parameter error'));
    }


     /**
     * 编辑
     * @throws Throwable
     */
    public function edit(): void
    {
        $id  = $this->request->param($this->model->getPk());
        $row = $this->model->find($id);
        if (!$row) {
            $this->error(('Record not found'));
        }

        $dataLimitAdminIds = $this->getDataLimitAdminIds();
        if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
            $this->error(('You have no permission'));
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $day = VipDay::find(intval($data['vip_day_id']));
            if (!$data) {
                $this->error(('Parameter %s can not be empty'));
            }

            $data   = $this->excludeFields($data);
            $data['min_price'] = sprintf("%.2f",floor($data['price']) / $day['day']);
            $result = false;
            $this->model->startTrans();
            try {
                // 模型验证
                if ($this->modelValidate) {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    if (class_exists($validate)) {
                        $validate = new $validate;
                        if ($this->modelSceneValidate) $validate->scene('edit');
                        $validate->check($data);
                    }
                }
                $result = $row->save($data);
                $this->model->commit();
            } catch (Throwable $e) {
                $this->model->rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success(('Update successful'));
            } else {
                $this->error(('No rows updated'));
            }
        }
        $this->success('', [
            'row' => $row
        ]);
    }
}
